package com.sky.mapper;


import com.github.pagehelper.Page;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.Orders;
import com.sky.vo.OrderReportVO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Mapper
public interface OrdersMapper {
    /**
     * 提交订单
     * @param orders
     */
    void insert(Orders orders);

//    /**
//     * 根据订单号查询订单
//     * @param orderNumber
//     */
//    @Select("select * from orders where number = #{orderNumber}")
//    Orders getByNumber(String orderNumber);

    /**
     * 修改订单信息
     * @param orders
     */
    void update(Orders orders);


    /**
     * 分页查询订单
     * @param ordersPageQueryDTO
     * @return
     */

    Page<Orders> getByQueryPage(OrdersPageQueryDTO ordersPageQueryDTO);

    /**
     * 订单详情
     * @param id
     * @return
     */
    @Select("select * from orders where id = #{id}")
    Orders getById(Long id);


    @Select("select * from orders where status = #{status} and order_time < #{time}")
    List<Orders> getByStatusWithOrderTimeTL(Integer status, LocalDateTime time);


    /**
     * 根据订单号查询订单
     * @param outTradeNo
     * @return
     */
    @Select("select * from orders where number = #{orderNumber}")
    Orders getByNumber(String outTradeNo);


    /**
     * 数据统计
     * @param begin
     * @param end
     * @return
     */
    @Select("select * from orders where order_time >= #{begin} and order_time <= #{end} and status = 5")
    List<Orders> getByTime(String begin, String end);

    /**
     * 用户统计
     * @param begin
     * @param end
     * @return
     */
    @Select("select count(user_id) as total_user_list ,count(distinct user_id) as new_user_list,date(order_time) as date_list from orders  where order_time between #{begin} and #{end} group by date(order_time)")
    List<UserReportVO> getUserStatistics(String begin, String end);


    /**
     * 视频有效订单统计
     * @param map
     * @return
     */
    Integer getOrdersStatistics(Map map);

    // 视频方法 : 用动态sql可扩展性强
    Double findByTime(Map map);

//    /**
//     * 有效订单统计
//     * @param begin
//     * @param end
//     * @return
//     */
//
//    @Select("SELECT   \n" +
//            "    DATE(order_time) AS date_list,   \n" +
//            "    COUNT(*) AS order_count_list,   \n" +
//            "    COUNT(CASE WHEN status = 5 THEN 1 END) AS valid_order_count_list \n" +
//            "FROM   \n" +
//            "    orders  \n" +
//            "WHERE   \n" +
//            "    order_time BETWEEN #{begin} AND #{end}  \n" +
//            "GROUP BY   \n" +
//            "    DATE(order_time)  \n" +
//            "ORDER BY   \n" +
//            "    date_list")
//    List<OrderReportVO> getOrdersStatistics(String begin, String end);



}
